'use strict';
// mongondb的一个模板库
import mongoose from 'mongoose';
import config from 'config-lite';
import chalk from 'chalk';
import log4js from 'log4js';

const log = log4js.getLogger("mongoose");

var db = mongoose.connect(config.mongodb).connection;

db.on('error', function(err) {
    console.error(
        chalk.red('Error in MongoDb connection: ' + err)
    );
    mongoose.disconnect();
});

// once() 执行一次监听器就会销毁
db.once('open' ,() => log.info("mogoose 连接成功"));

db.on('close', function() {
    log.debug(
      chalk.red('mongoose断开，正在重连')
    );
    mongoose.connect(config.mongodb, {server:{auto_reconnect:true}});
});

export default db;
